﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfCallScreen.Common
{
    public static class CallHelper
    {
        public static bool isOpen=true;
        static CallHelper()
        {
            var uristr = "/video/ling.wav"; //file资源放在Documents文件夹下
            var uri = new Uri(uristr, UriKind.Relative);
            sp.Stream = Application.GetResourceStream(uri).Stream;

        }
        public static SoundPlayer sp = new SoundPlayer();

        public static ConcurrentQueue<string> MsgQueue = new ConcurrentQueue<string>();

        /// <summary>
        /// 语音播放锁
        /// </summary>
        public readonly static object playLockObj = new object();

        public static void PutMsg(string Msg)
        {
            MsgQueue.Enqueue(Msg);
        }
        public static void Play()
        {
            Task.Run(() =>
            {
                while (isOpen)
                {
                    lock (playLockObj)
                    {
                        string item;
                        if (MsgQueue.TryDequeue(out item))
                        {
                            sp.Play();
                            using (SpeechSynthesizer speech = new SpeechSynthesizer())
                            {
                                speech.Rate = 0;
                                speech.Volume = 100;
                                speech.Speak(item);
                            }
                             
                        }
                        Task.Delay(100);
                    }
                }
            });
        }
    }
}
